package com.level2app.ui.search;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.level2app.R;
import com.level2app.adapter.RecentHistoryAddressAdapter;
import com.level2app.adapter.SearchAddressAdapter;
import com.level2app.base.BaseActivity;
import com.level2app.databinding.ActivitySearchAddressBinding;
import com.level2app.ui.search.view.SearchAddressViewModel;
import com.level2app.utils.KeyboardUtils;
import com.level2app.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索地址
 */
public class SearchAddressActivity extends BaseActivity<ActivitySearchAddressBinding, SearchAddressViewModel> {

    /**
     * 定位权限是否开启
     */
    private boolean isTurnOn = true;
    private RecentHistoryAddressAdapter adapter;
    private SearchAddressAdapter searchAddressAdapter;
    private List<String> data = new ArrayList<>();

    @Override
    protected SearchAddressViewModel bindModel() {
        return getViewModel(this, SearchAddressViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.address));
        binding.setMViewModel(viewModel);
        binding.currentAddressTv.setText(getString(R.string.current_address, "qqqqqqq"));
        data.add("");
        data.add("");
        data.add("");
        adapter = new RecentHistoryAddressAdapter();
        binding.recyclerView.setAdapter(adapter);
        adapter.submitList(data);

        if (isTurnOn) {
            binding.accessSwitchTv.setBackgroundResource(R.drawable.shape_90dp_dbdbdb);
        } else {
            binding.accessSwitchTv.setBackgroundResource(R.drawable.shape_btn_bg);
        }
        searchAddressAdapter = new SearchAddressAdapter();
        binding.searchRv.setLayoutManager(new LinearLayoutManager(this));
        binding.searchRv.setAdapter(searchAddressAdapter);
    }

    @Override
    protected void initClick() {
        super.initClick();
        binding.mInputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.deleteIv.setVisibility(charSequence.toString().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())){
                    binding.searchRv.setVisibility(View.GONE);
                    //TODO:清空数据
                }
            }
        });
        viewModel.onDelayClick(binding.deleteIv, o -> binding.mInputEt.setText(""));

        viewModel.onDelayClick(binding.refreshLocationLl, o -> {
            ToastHelper.showToast("刷新当前定位");
        });

        viewModel.onDelayClick(binding.accessSwitchTv, o -> {
            isTurnOn = !isTurnOn;
            binding.accessSwitchTv.setText(getString(isTurnOn ? R.string.turn_on : R.string.turn_off));
            binding.accessSwitchTv.setBackgroundResource(isTurnOn ? R.drawable.shape_90dp_dbdbdb : R.drawable.shape_btn_bg);
            binding.locationAccessStatusTv.setText(getString(isTurnOn ? R.string.location_access_off : R.string.location_access_on));
            ToastHelper.showToast(getString(isTurnOn ? R.string.location_access_off : R.string.location_access_on));
        });

        // 按下回车搜索
        binding.mInputEt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                if (!TextUtils.isEmpty(binding.mInputEt.getText().toString().trim())) {
                    KeyboardUtils.hideSoftInput(SearchAddressActivity.this);
                    ToastHelper.showToast("干啥呀");
                    binding.searchRv.setVisibility(View.VISIBLE);
                    searchAddressAdapter.submitList(data);
                }
                return true;
            }
            return false;
        });

        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            KeyboardUtils.hideSoftInput(SearchAddressActivity.this);
            ToastHelper.showToast("点击item");
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}